import { individualRegUrl, smsCodeUrl, serveUrl } from '../../api/index'
import { post, previewDocument } from '../../utils/index'
import WxValidate from '../../utils/WxValidate'
import Notify from '@vant/weapp/notify/notify'

Page({
  data: {
    form: {
      name: '',
      phone: '',
      code: '',
      password: '',
      password2: '',
      companyName: '', // 企业名称
      socialCreditCode: '', // 统一社会信用代码
      systemType: 'dispatch'
    },
    userType: 'individual',
    formError: {},
    timer: null,
    second: 60,
    btnName: '发送验证码'
  },
  async register () { // 注册操作
    const { form, userType, timer } = this.data
    if (!this.WxValidate.checkForm(form)) {
      const formError = this.WxValidate.formError
      this.setData({ formError })
      return
    }
    if (form.password !== form.password2) {
      this.setData({ formError: { password2: '两次密码不一致' } })
      return
    }
    this.setData({ formError: {} })
    await post(individualRegUrl, form)
    clearInterval(timer)
    wx.showToast({
      title: '恭喜注册成功，请登录',
      icon: 'none',
      success: () => {
        setTimeout(() => {
          wx.navigateBack({})
        }, 1000)
      }
    })
  },
  async sendCode () { // 发送验证码
    const { phone } = this.data.form
    this.setData({ formError: {} })
    if (!this.WxValidate.checkFormField({ phone })) {
      const formError = this.WxValidate.formError
      this.setData({ formError })
      return
    }
    await post(smsCodeUrl, { phone }, {}, false)
    this.countDown()
    Notify({ type: 'success', message: `将向手机号${phone}发送短信验证码，请注意查收` })
  },
  // 60s倒计时
  countDown () {
    let { timer, second, btnName } = this.data
    timer = setInterval(() => {
      if (second === '00') {
        clearInterval(timer)
        second = 60
        btnName = '发送验证码'
      } else {
        second--
        second = second < 10 ? `0${second}` : `${second}`
        btnName = `${second}s后重发`
      }
      this.setData({ timer, second, btnName })
    }, 1000)
  },
  initValidator () { // 初始化验证器
    const rules = {
      name: { required: true, maxlength: 16 },
      phone: { required: true, tel: true },
      code: { required: true },
      password: { required: true, password: true, maxlength: 32 }
    }
    const messages = {
      name: { required: '请输入姓名', maxlength: '姓名长度不大于16位' },
      phone: { required: '请输入手机号码', tel: '请填写正确手机号' },
      code: { required: '请输入短信验证码' },
      password: { required: '请输入密码', password: '密码必须包含字母，数字，符号中至少两种(不含空格)', maxlength: '密码长度不大于32位' }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  changeTab (e) {
    const { tab } = e.target.dataset
    this.setData({
      userType: tab
    })
  },
  nameChange ({ detail }) { // 姓名
    this.data.form.name = detail
  },
  phoneChange (e) { // 手机号
    this.data.form.phone = e.detail
  },
  codeChange (e) { // 验证码
    this.data.form.code = e.detail
  },
  pwdChange (e) { // 密码
    this.data.form.password = e.detail
  },
  pwd2Change (e) { // 确认密码
    this.data.form.password2 = e.detail
  },
  onChange ({ detail }) { // 同意条款
    this.setData({ isRead: detail })
  },
  handleRead () { // 阅读条款
    previewDocument(serveUrl)
  },
  onClickLeft () { // 返回
    clearInterval(this.data.timer)
    wx.navigateBack({})
  },
  onLoad () {
    this.initValidator()
  }
})